<?php

namespace app\admin\controller\factor;

use app\common\controller\Backend;

/**
 * 碳排放因子库
 *
 * @icon fa fa-circle-o
 */
class CarbonFactorUser extends Backend
{

    /**
     * CarbonFactorUser模型对象
     * @var \app\admin\model\factor\CarbonFactor
     */
    protected $model = null;
    /**
     * @var array|string
     */
    private $usernameList;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\factor\CarbonFactor;
        $this->usernameList = \app\admin\model\User::where('status','normal')->column('nickname','id');

        $lifecycleType = $this->model->getLifecycleType();
        $statusList =  $this->model->getStatusList();
        $checkStatus = $this->model->getCheckStatusList();


        $this->view->assign("usernameList",$this->usernameList);
        $this->view->assign("lifecycleType",$lifecycleType);
        $this->view->assign("statusList",$statusList);
        $this->view->assign("checkStatus",$checkStatus);

        $this->assignconfig('usernameList', $this->usernameList);
        $this->assignconfig('lifecycleType', $lifecycleType);
        $this->assignconfig('statusList', $statusList);
        $this->assignconfig('checkStatus', $checkStatus);

        $categoryModel = new \app\admin\model\factor\Category;
        $categoryNameList = $categoryModel->column('name','id');
        $categoryNameList[0] = '无';

        $this->view->assign('categoryNameList', $categoryNameList);
        $this->assignconfig('categoryNameList', $categoryNameList);
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }

        [$where, $sort, $order, $offset, $limit] = $this->buildparams();

        $list = $this->model
            ->where($where)
            ->where('user_id','>',0)
            ->order($sort, $order)
            ->paginate($limit);
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    public function add()
    {
        $this->error();
    }


    public function detail($ids)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $row->username = $this->usernameList[$row->user_id] ?? '';

        $lastReview = $row->getLastFactorReviewAttr();
        $lastAccount = $row->getLastAccountAuthAttr();
        $row->account_reason = $lastAccount->reason ?? '';
        $row->review_reason = $lastReview->reason ?? '';
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    public function edit($ids = null)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a", [], 'strip_tags');
            if ($params) {

                if (!$row->sn) {
                    $row->sn = $this->model->getFactorSn();
                }
                if (!$row->is_authority) {
                    if ($params['is_authority']) {
//                    if (!$params['authority_reason']) {
//                        $this->error('请填写驳回的原因');
//                    }
                        $row->authority_time = time();
                        $row->is_authority = $params['is_authority'];
                    }

                    if ($params['status'] == 1) {
                        if ($params['product_review'] != 1 && $row->status == 0) {
                            $this->error('请先能完成上架审核');
                        }
                        $row->down_type = 0;
                        $row->down_reason = '';
                    } else {
                        if ( $row->status == 1) {
                            if (!$params['down_reason']) {
                                $this->error('请填写下架原因');
                            }
                            $row->down_type = 1;
                            $row->down_reason = $params['down_reason'];
                        }
                    }

                    $row->status = $params['status'];

                    $row->save();
                }

                $this->success();
            }
            $this->error();
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    public function getSnapshot($ids = null)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $this->view->assign("snapshot", $row->snapshot);
        return $this->view->fetch();
    }

    public function toggle($ids = null)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            if ($row->status == 0) {
                if ($row->product_review != 1) {
                    $this->error('当前产品正在审核中，无法上架');
                }
                $row->status = 1;
            } else {
                $row->status = 0;
            }
            $row->save();
            $this->success();
        }
        $this->error('请求错误');
    }
}
